﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace YArchitech.Plumbing
{
	internal class HotWaterCalBook
	{
		public bool Go(object filepath, List<HotUsedWaterCal> calculateOjects, string szHeat, string szWater)
		{
			Application application = (Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
			object obj = filepath;
			if (File.Exists((string)obj))
			{
				File.Delete((string)obj);
			}
			object value = Missing.Value;
			object obj2 = 25;
			Document document = application.Documents.Add(ref value, ref value, ref value, ref value);
			string text = "按照《建筑给水排水设计规范》：\n";
			if (HotUsedWaterCalManage.Instance.bIsCalForDay)
			{
				text += "Qh = Kh×m×qr×C×(tr-t1)×ρr/T\n";
				text += "式中：\n";
				text += "     Qh -- 设计小时耗热量（KJ/h）；\n";
				text += "     m -- 用水计算单位数（人数或床位数）；\n";
				text += "     qr -- 热水用水定额（L/人·d或L/床·d）；\n";
				text += "     C -- 水的比热 ，C=4.187（KJ/kg·℃）；\n";
				text += "     tr -- 热水温度，tr=60℃；\n";
				text += "     tl -- 冷水温度，tl=10℃；\n";
				text += "     ρr -- 热水密度（kg/L），ρr＝0.9832；\n";
				text += "     T -- 每日使用时间（h）；\n";
				text += "     Kh -- 小时变化系数；\n";
				obj2 = 25;
			}
			else
			{
				text += "Qh = ∑qh×(tr-t1)×ρr×n0×b×C\n";
				text += "式中：\n";
				text += "     Qh -- 设计小时耗热量（KJ/h）；\n";
				text += "     qh -- 卫生器具热水的小时用水定额（L/h）；\n";
				text += "     C -- 水的比热 ，C=4.187（KJ/kg·℃）；\n";
				text += "     tr -- 热水温度，tr=60℃；\n";
				text += "     tl -- 冷水温度，tl=10℃；\n";
				text += "     ρr -- 热水密度（kg/L），ρr＝0.9832；\n";
				text += "     n0 -- 同类型卫生器具数；\n";
				text += "     b -- 卫生器具的同时使用百分数；\n";
				obj2 = 24;
			}
			text += "\n";
			text += "qrh = Qh/((tr-t1)×C×ρr)\n";
			text += "式中：\n";
			text += "     qrh -- 设计小时热水量（L/h）；\n";
			text += "     Qh -- 设计小时耗热量（KJ/h）；\n";
			text += "     tl -- 设计热水温度，tl=60℃；\n";
			text += "     tr -- 设计冷水温度，tr=10℃；\n";
			text += "\n";
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
			document.Paragraphs.Last.Range.Text = "热水小时用水量计算书\n";
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			document.Paragraphs.Last.Range.Text = text;
			text = "各用水部位统计结果如下\n";
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			document.Paragraphs.Last.Range.Text = text;
			object obj3 = WdUnits.wdParagraph;
			Selection selection = application.Selection;
			object obj4 = value;
			selection.MoveDown(ref obj3, ref obj2, ref obj4);
			Table table = document.Tables.Add(application.Selection.Range, calculateOjects.Count + 1, 3, ref value, ref value);
			table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
			table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
			table.Columns[1].Width = 200f;
			table.Columns[2].Width = 80f;
			table.Columns[3].Width = 50f;
			table.Cell(1, 1).Range.Text = "用水部位";
			table.Cell(1, 2).Range.Text = "用水标准";
			table.Cell(1, 3).Range.Text = "数量";
			application.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
			application.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			int num = 2;
			foreach (HotUsedWaterCal hotUsedWaterCal in calculateOjects)
			{
				table.Cell(num, 1).Range.Text = hotUsedWaterCal.typeName;
				table.Cell(num, 2).Range.Text = hotUsedWaterCal.maxUsedProposal.ToString();
				table.Cell(num, 3).Range.Text = hotUsedWaterCal.usedCount.ToString();
				num++;
			}
			application.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			document.Paragraphs.Last.Range.Text = "\n总计如下";
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			document.Paragraphs.Last.Range.Text = "设计小时热水量：" + szWater + "(L/h)";
			document.Paragraphs.Add(ref value);
			document.Paragraphs.Last.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
			document.Paragraphs.Last.Range.Text = "设计小时耗热量：" + szHeat + "(KJ/h)";
			document.SaveAs(ref obj, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value, ref value);
			document.Close(ref value, ref value, ref value);
			application.Quit(ref value, ref value, ref value);
			return true;
		}
	}
}
